package xyz.redtorch.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;

import xyz.redtorch.core.entity.SubscribeReq;
import xyz.redtorch.web.annotation.Authorization;
import xyz.redtorch.web.controller.base.BaseController;
import xyz.redtorch.web.core.exception.ResourcesNotFoundException;
import xyz.redtorch.web.core.vo.ResultVO;
import xyz.redtorch.web.service.DataRecordWebService;

import java.util.Optional;

@RestController
@RequestMapping("${api.base.path}/dataRecord")
public class DataRecordController extends BaseController {

	@Autowired
	DataRecordWebService dataRecordWebService;
	
	@RequestMapping(path = "/subscribeReqs")
//	@ResponseBody
	@Authorization
	public ResultVO getSubscribeReqs() {
		ResultVO result = new ResultVO();
		result.setData(dataRecordWebService.getSubscribeReqs());
		return result;
	}
	
	@RequestMapping(value = "/subscribeReq", method = RequestMethod.PUT)
//	@ResponseBody
	@Authorization
	public ResultVO saveOrUpdateSubscribeReq(@RequestBody SubscribeReq subscribeReq) {
		Optional.ofNullable(subscribeReq).orElseThrow(new ResourcesNotFoundException("请求体参数不允许为空"));
		ResultVO result = new ResultVO();
		dataRecordWebService.saveOrUpdateSubscribeReq(subscribeReq);
		return result;
	}
	
	@RequestMapping(value = "/subscribeReq", method = RequestMethod.DELETE)
//	@ResponseBody
	@Authorization
	public ResultVO deleteSubscribeReq(@RequestBody JSONObject jsonObject) {
		ResultVO result = new ResultVO();
		checkParams(jsonObject, new String[]{"subscribeReqID"});
		dataRecordWebService.deleteSubscribeReq(jsonObject.getString("subscribeReqID"));
		return result;
	}
	
}
